package com.spring.demo.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.fabric.xmlrpc.base.Array;
import com.spring.demo.dao.mybatis.UserMapper;
import com.spring.demo.dao.user.UserDAO;
import com.spring.demo.model.user.User;
import com.spring.demo.service.user.UserService;

@Service(UserService.SERVICE_NAME)
public class UserServiceImpl implements UserService {

	@Resource(name = UserDAO.DAO_NAME)
	private UserDAO userDAO;
	
	@Resource(name = "userMapper")
	private UserMapper userMapper;

	@Override
	public List<User> getUser() {
		// TODO Auto-generated method stub
		/*String selectSql = "select * from user where dr=0";
		return userDAO.selectUser(selectSql);*/
		//return userMapper.selectUsers();
		User user = userMapper.selectUserById(2L);
		List<User> li = new ArrayList<>();
		li.add(user);
		
		li = userMapper.selectUser(2L, "2");
		return li;
	}

	@Override
	@Transactional
	public boolean registerUser(User user) {
		// TODO Auto-generated method stub
		if(user!=null){
			user.setIsOnline(false);
			String insertSql = "insert into user (user_name,pass_word,type,is_online,age,leader_id,dr) values (?,?,?,?,?,?);";
			return userDAO.saveOrUpdateUser(insertSql, user.getUserName(),user.getPassWord(),user.getType(),user.getIsOnline(),user.getAge(),user.getLeader(),user.getDr());
		}else{
			return false;
		}
		
	}

	@Override
	public User loginUser(String userName,String passWord) {
		// TODO Auto-generated method stub
		String selectSql = "select * from user where user_name=? and pass_word=?";
		List<User> list = userDAO.selectUser(selectSql, userName, passWord);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<User> selectUser(String userName,Long id) {
		// TODO Auto-generated method stub
		StringBuffer selectSql = new StringBuffer("select * from user where 1=1 and dr=0");
		List<Object> params = new ArrayList<Object>();
		if(userName!=null){
			selectSql.append(" and user_name=?");
			params.add(userName);
		}
		if(id!=null){
			selectSql.append(" and id=?");
			params.add(id);
		}
		return userDAO.selectUser(selectSql.toString(), params.toArray());
	}

	@Override
	public boolean updateUser(User user) {
		// TODO Auto-generated method stub
		if(user!=null&&user.getId()!=null){
			StringBuffer updateSql = new StringBuffer("UPDATE user SET is_online = ?, last_login_time = ? where id = ?");
			List<Object> params = new ArrayList<>();
			params.add(user.getIsOnline());
			params.add(user.getLastLoginTime());
			return userDAO.saveOrUpdateUser(updateSql.toString(), params);
		}
		return false;
	}

	@Override
	public List<User> testMybatis() {
		// TODO Auto-generated method stub
		User user = new User();
		user.setAge(2);
		user.setDr(0);
		user.setIsOnline(false);
		user.setLeader(1L);
		user.setPassWord("mybatisTest222");
		user.setUserName("mybatisTest");
		user.setType(1);
	//	int a = userMapper.insertUser(user);
		user.setId(6L);
		//int a = userMapper.updateUser(user);
		int a = userMapper.deleteUser(6L);
		System.out.println(a);
		return null;
	}
	
}

